package com.sip.stream.utils.zfy.consumer;

import android.media.MediaScannerConnection;

import com.sip.stream.ClientApp;
import com.sip.stream.media.ZFYMediaManager;
import com.sip.stream.utils.zfy.WavHeaderHelper;
import com.sip.stream.utils.zfy.ZFYMediaInfo;
import com.sip.stream.utils.zfy.config.ZFYMediaConfig;
import com.sip.stream.utils.zfy.muxer.IGSMediaMuxer;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

public abstract class GSWavConsumer implements IGSMediaMuxer, IGSWAVConsumer {
    protected static String TAG = "GSWavConsumer";
    private int bitsPreSample;
    private int byteRate;
    private int chanelCount;
    private long endTime;
    private FileOutputStream fileOutputStream;
    private boolean isRecording = false;
    private int sampleRate;
    private IGSMediaObserver<IGSWAVConsumer> wavConsumerObserver;
    private WavHeaderHelper wavHeaderHelper;
    protected ZFYMediaInfo zfyMediaInfo;

    public abstract ZFYMediaInfo createNewRecording();

    public void doAfterReleaseWAV(String str) {
    }

    public GSWavConsumer() {
        this.sampleRate = 0;
        this.byteRate = 0;
        ZFYMediaConfig config = ZFYMediaManager.getInstance().getConfig();
        this.sampleRate = config.getAudioSampleRate();
        this.chanelCount = config.getAudioChanelCount();
        if (config.getAudioFormate() == 3) {
            this.bitsPreSample = 8;
        } else {
            this.bitsPreSample = 16;
        }
        this.byteRate = this.bitsPreSample * this.sampleRate;
        this.wavHeaderHelper = new WavHeaderHelper();
        start();
    }

    private synchronized FileOutputStream startNewRecording() {
        FileOutputStream fileOutputStream;
        this.zfyMediaInfo = createNewRecording();
        this.endTime = System.currentTimeMillis() + 3600000;
        ZFYMediaManager.getInstance().getStatus().setBeginRecordingDate(new Date());
        try {
            fileOutputStream = new FileOutputStream(new File(this.zfyMediaInfo.getFilepath()));
            try {
                fileOutputStream.write(this.wavHeaderHelper.getWaveFileHeader(this.sampleRate, this.chanelCount, this.bitsPreSample, this.byteRate / 8, 0L));
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        } catch (FileNotFoundException e2) {
            e2.printStackTrace();
            return null;
        }
        return fileOutputStream;
    }

    @Override
    public void start() {
        this.fileOutputStream = startNewRecording();
        this.isRecording = true;
    }

    private void releaseWAV() throws IOException {
        String filename;
        FileOutputStream fileOutputStream = this.fileOutputStream;
        if (fileOutputStream == null) {
            return;
        }
        fileOutputStream.close();
        File file = new File(this.zfyMediaInfo.getFilepath());
        this.wavHeaderHelper.updateWavFileHeader(file.getAbsolutePath(), file.length());
        String[] split = this.zfyMediaInfo.getFilename().split("\\.");
        if (split.length > 1) {
            filename = split[0];
        } else {
            filename = this.zfyMediaInfo.getFilename();
        }
        if (ZFYMediaManager.getInstance().getStatus().isImportantAndUse()) {
            filename = addImpMark(filename);
        }
        String str = filename + ".wav";
        String replace = this.zfyMediaInfo.getFilepath().replace(this.zfyMediaInfo.getFilename(), str);
        file.renameTo(new File(replace));
        this.zfyMediaInfo.setFilepath(replace);
        this.zfyMediaInfo.setFilename(str);
        this.zfyMediaInfo.setComplete(true);
        this.zfyMediaInfo.setFilesize(file.length());
        MediaScannerConnection.scanFile(ClientApp.clientApp, new String[]{replace}, new String[]{"audio/x-wav"}, null);
        doAfterReleaseWAV(replace);
    }

    private String addImpMark(String str) {
        return "IMP_" + str;
    }

    private String addImpMark_GXGA(String str) {
        return str.replace("NOR", "IMP");
    }

    @Override
    public void stop() {
        IGSMediaObserver<IGSWAVConsumer> iGSMediaObserver = this.wavConsumerObserver;
        if (iGSMediaObserver != null) {
            iGSMediaObserver.onStop(this);
        }
        this.endTime = System.currentTimeMillis();
        this.isRecording = false;
        try {
            releaseWAV();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void onAudioStream(byte[] bArr, long j) {
        if (this.isRecording) {
            try {
                getFileOutputStream().write(bArr);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void addObserver(IGSMediaObserver<IGSWAVConsumer> iGSMediaObserver) {
        this.wavConsumerObserver = iGSMediaObserver;
    }

    private FileOutputStream getFileOutputStream() {
        if (System.currentTimeMillis() > this.endTime) {
            try {
                releaseWAV();
            } catch (IOException e) {
                e.printStackTrace();
            }
            this.fileOutputStream = startNewRecording();
        }
        return this.fileOutputStream;
    }

    @Override
    public void forceSwitchMuxer() {
        this.endTime = System.currentTimeMillis();
    }
}
